package com.pangda.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pangda.common.constant.BusinessStatusConstant;
import com.pangda.common.domain.PageResult;
import com.pangda.common.domain.ResponseResult;
import com.pangda.pojo.Business;
import com.pangda.mapper.BusinessMapper;
import com.pangda.pojo.SysUser;
import com.pangda.pojo.dto.BusinessDto;
import com.pangda.pojo.dto.ContractDto;
import com.pangda.pojo.vo.SalesRankVo;
import com.pangda.service.IBusinessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pangda.service.IContractService;
import com.pangda.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商机管理 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-21
 */
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements IBusinessService {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private IContractService contractService;

    /**
     * 商机分配
     *
     * @param businessDto 信息
     * @return
     */
    @Override
    public ResponseResult assignment(BusinessDto businessDto) {
        if (businessDto == null || businessDto.getIds().isEmpty() || businessDto.getUserId() == null) {
            return ResponseResult.error("参数错误");
        }
        SysUser byId = sysUserService.getById(businessDto.getUserId());
        LambdaUpdateWrapper<Business> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(Business::getId, businessDto.getIds())
                .set(Business::getOwner, byId.getUserName())
                .set(Business::getUserId, businessDto.getUserId())
                .set(Business::getOwnerTime, LocalDateTime.now());
        return update(wrapper) ? ResponseResult.success() : ResponseResult.error("分配失败");
    }

    /**
     * 客户统计列表
     *
     * @param pageResult  分页参数
     * @param businessDto 查询条件
     * @return
     */
    @Override
    public PageResult contractStatisticsList(PageResult pageResult, BusinessDto businessDto) {
        Page<Business> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(businessDto.getCreateBy()), Business::getCreateBy, businessDto.getCreateBy())
                .between(ObjectUtil.isNotEmpty(businessDto.getEndCreateTime()) && ObjectUtil.isNotEmpty(businessDto.getBeginCreateTime()),
                        Business::getCreateTime, businessDto.getBeginCreateTime(), businessDto.getEndCreateTime())
                .eq(ObjectUtil.isNotEmpty(businessDto.getChannel()), Business::getChannel, businessDto.getChannel())
                .eq(Business::getStatus, BusinessStatusConstant.CLIENT);
        page(page, wrapper);
        return PageResult.success(page, Business.class);
    }

    @Override
    public PageResult deptStatisticsList(PageResult pageResult, BusinessDto businessDto) {
        return null;
    }

    @Override
    public List<SalesRankVo> getRank(String beginCreateTime, String endCreateTime) {
        endCreateTime = endCreateTime + " 23:59:59";
        List<SalesRankVo> list = businessMapper.getRank(beginCreateTime, endCreateTime);
        return list;
    }
}
